Introduction

We are going to reproduce three different plots, of which we are only given the data set and the final image file. Each of them has two versions:

  • The first version is a plot made just using the layer functions of ggplot2 and leaving everything else with the defaults. This is, using ggplot, geom_*, stat_* and annotate_* functions (click on the image to see it bigger).

random image random image random image

  • The second version builds from the first plot to create a more complex display with customized scales_*, coord_*, facet_* or theme_*. Helper functions such as labs() or guides() could also be used (click on the image to see it bigger).

random image random image random image

library(ggplot2)

: ggplot2 will automatically load all three data sets used in this practical.

Organization of the practical

You will see different icons through the document, the meaning of which is:

: additional or useful information
: a worked example
: a practical exercise
: a space to answer the exercise
: a hint to solve an exercise
: a more challenging exercise


Simple versions

Plot 1

Answer:
# Dataset used: diamonds
ggplot(data=diamonds, aes(x=clarity, y=log10(carat)))+
  geom_point(aes(color=cut), position=position_jitter(width=0.4))+
  geom_violin(fill="black")

Plot 2

Need help with the count numbers shown in each stacked bar? https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2

Answer:
# Dataset used: mpg
ggplot(data=mpg, mapping = aes(x=manufacturer, fill = class))+
  geom_bar()+
  geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5))
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Plot 3

Answer:
# Dataset: ToothGrowth
ggplot(data=ToothGrowth, aes(x=as.factor(dose), y=len))+
  geom_boxplot(aes(color=supp, fill=supp), color="black")+
  geom_hline(yintercept = mean(ToothGrowth$len), lty="dashed")


Complex versions

Plot 1

Answer:
ggplot(data=diamonds, aes(x=clarity, y=log10(carat)))+
  geom_point(aes(color=cut), position=position_jitter(width=0.4))+
  geom_violin(fill="black")+
  facet_wrap(cut ~ ., ncol=5)+ggtitle("Diamond quality")+
  theme_bw()+theme(legend.position="none")

Plot 2

Answer:
ggplot(data=mpg, aes(x=manufacturer, fill = class))+
  geom_bar()+
  geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5))+
  coord_flip()+scale_fill_brewer(type="qual", palette = 2)+
  labs(x="Manufacturer", y="Number of cars", fill = "Car type")

This graph uses a scale_fill_brewer palette, can you find which one?

Plot 3

Answer:
ggplot(data=ToothGrowth, aes(x=as.factor(dose), y=len))+
  geom_boxplot(aes(colour = supp, fill = supp), color="black")+
  geom_hline(data= ToothGrowth, yintercept= mean(ToothGrowth$len), lty = "dashed")+
  scale_fill_brewer(type="seq", palette=7, direction=-1, label= c("Orange juice", "Vitamin C"))+
  labs(y="Tooth length", x="Dose (mg/day)", fill="Supplement")+
  theme_classic()

This graph uses a scale_fill_brewer palette, can you find which one?